package com.cgbl.cg.ui.home

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.HomeFollowAdapter
import com.cgbl.cg.adapter.HomeVisitAdapter
import com.cgbl.cg.databinding.AppFragmentFollowBinding
import com.cgbl.cg.vm.home.HomeFollowViewModel
import com.robot.base.base.activity.GLBaseFragment

class HomeFollowFragment : GLBaseFragment<AppFragmentFollowBinding, HomeFollowViewModel>() {

    private lateinit var homeVisitAdapter: HomeVisitAdapter
    private lateinit var homeFollowAdapter: HomeFollowAdapter

    override fun initContentView(
        inflater: LayoutInflater?,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): Int = R.layout.app_fragment_follow

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {
        initAdapter()
        viewModel.initData()
    }

    private fun initAdapter() {
        homeVisitAdapter = HomeVisitAdapter()
        binding.mRvVisit.apply {
            adapter = homeVisitAdapter
        }

        homeFollowAdapter = HomeFollowAdapter()
        binding.mRvFollow.apply {
            adapter = homeFollowAdapter
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.visitData.observe(this) {
            if (it.size > 13) {
                homeVisitAdapter.setNewInstance(it.subList(0, 13))
            } else {
                homeVisitAdapter.setNewInstance(it)
            }
            homeVisitAdapter.addData("")
        }

        viewModel.uc.followData.observe(this) {
            homeFollowAdapter.addData(it)
        }
    }
}